package com.gwt.mvp.sample.client;

import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasWidgets;
import com.gwt.mvp.sample.client.event.ResetViewEvent;
import com.gwt.mvp.sample.client.event.ResetViewEvent.ResetViewEventHandler;
import com.gwt.mvp.sample.client.presenter.EntityPresenter;
import com.gwt.mvp.sample.client.presenter.Presenter;
import com.gwt.mvp.sample.client.view.EntityView;

public class AppController implements Presenter {
	
	private enum ActionType{
		RESET_VIEW;
	}
	
	private final HandlerManager eventBus;
	private HasWidgets container;

	@Override
	public void go(HasWidgets container) {
		this.container = container;
		managePresenter(ActionType.RESET_VIEW);
	}
	
	public AppController(HandlerManager eventBus) {
		this.eventBus = eventBus;
		bind();
	}
	
	private void bind(){
		
		//Register all the handlers
		eventBus.addHandler(ResetViewEvent.TYPE, new ResetViewEventHandler() {
			@Override
			public void onIncrementEntity(ResetViewEvent event) {
				managePresenter(ActionType.RESET_VIEW);
			}
		});
		
	}
	
	private void managePresenter(ActionType actionType){
		if (actionType != null){
			Presenter presenter = null;
			switch(actionType){
				case RESET_VIEW : presenter = new EntityPresenter(eventBus, new EntityView());
											  break;
			}
			if(presenter != null)
				presenter.go(container);
		}
	}
	
}
